import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerPositionExperienceApi } from '#/api/per/positionExperience';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },

    {
      fieldName: 'workUnit',
      label: '工作单位',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入工作单位',
      },
    },
    {
      fieldName: 'startDate',
      label: '开始时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'endDate',
      label: '结束时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'positionName',
      label: '职务名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入职务名称',
      },
    },
    {
      fieldName: 'positionLevel',
      label: '职务层次',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_POSITION_LEVEL),
        placeholder: '请输入职务层次',
      },
    },

    {
      fieldName: 'teachingSubject',
      label: '任教学科',
      component: 'Input',
      componentProps: {
        placeholder: '请输入任教学科',
      },
    },
    {
      fieldName: 'isLeader',
      label: '是否领导',
      component: 'RadioGroup',
      defaultValue: true,
      componentProps: {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },

    {
      fieldName: 'remarks',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '工作单位',
      field: 'workUnit',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '开始时间',
      field: 'startDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '结束时间',
      field: 'endDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '职务名称',
      field: 'positionName',
    },
    {
      label: '职务层次',
      field: 'positionLevel',
    },
    {
      label: '身份类别',
      field: 'identityTypeName',
    },
    {
      label: '是否领导（1：是，0：否）',
      field: 'isLeader',
    },
    {
      label: '任教学科',
      field: 'teachingSubject',
    },
    {
      label: '是否有效（1：有效，0：无效）',
      field: 'statusName',
    },
    {
      label: '备注',
      field: 'remarks',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'workUnit',
      label: '工作单位',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入工作单位',
      },
    },
    {
      fieldName: 'startDate',
      label: '开始时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'endDate',
      label: '结束时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'positionName',
      label: '职务名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入职务名称',
      },
    },
    {
      fieldName: 'positionLevel',
      label: '职务层次',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_POSITION_LEVEL),
        allowClear: true,
        placeholder: '请输入职务层次',
      },
    },

    {
      fieldName: 'teachingSubject',
      label: '任教学科',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入任教学科',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerPositionExperienceApi.PositionExperience>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },

    {
      field: 'workUnit',
      title: '工作单位',
      minWidth: 120,
    },
    {
      field: 'startDate',
      title: '开始时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'endDate',
      title: '结束时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'positionName',
      title: '职务名称',
      minWidth: 120,
    },
    {
      field: 'positionLevel',
      title: '职务层次',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_POSITION_LEVEL },
      },
    },

    {
      field: 'isLeader',
      title: '是否领导',
      minWidth: 80,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'teachingSubject',
      title: '任教学科',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '是否有效',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },

    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
